﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FlexiCommerce.Components;
using System.Web.DynamicData;

namespace FlexiCommerce.Web.Admin.FlexiAdmin.DynamicData.Content
{
    public partial class CategoryDetails : System.Web.UI.UserControl
    {
        protected override void OnInit(EventArgs e)
        {
            if(DetailsType == typeof(FlexiCommerce.Components.CategoryDetails))
            {
                DomainDataSource1.SelectMethod = "AllCategoryDetails";
                this.DomainDataSource1.SetTable(MetaModel.Default.GetTable(typeof(FlexiCommerce.Components.CategoryDetails)));
            }
            else if (DetailsType == typeof(FlexiCommerce.Components.ProductDetails))
            {
                DomainDataSource1.SelectMethod = "AllProductDetails";
                this.DomainDataSource1.SetTable(MetaModel.Default.GetTable(typeof(FlexiCommerce.Components.ProductDetails)));
            }
            
            base.OnInit(e);
        }
        protected void Page_Load(object sender, EventArgs e)
        {

        }


        public int DetailsID
        {
            get { return (int)ViewState[this.ID + "DetailsID"]; }
            set { ViewState[this.ID + "DetailsID"] = value; }
        }

        public Type DetailsType
        {
            get { return (Type)ViewState[this.ID + "DetailsType"]; }
            set { ViewState[this.ID + "DetailsType"] = value; }
        }

        protected void DomainDataSource1_Queried(object sender, System.Web.DomainServices.WebControls.DomainDataSourceQueriedArgs e)
        {

        }

        protected void DomainDataSource1_QueryCreated(object sender, Microsoft.Web.Data.UI.WebControls.QueryCreatedEventArgs e)
        {
            if(DetailsType == typeof(FlexiCommerce.Components.CategoryDetails))
            {
                e.Query = ((IQueryable<FlexiCommerce.Components.CategoryDetails>)e.Query).Where(c=> c.ID == DetailsID);
            }
            else if (DetailsType == typeof(FlexiCommerce.Components.ProductDetails))
            {
                e.Query = ((IQueryable<FlexiCommerce.Components.ProductDetails>)e.Query).Where(p=> p.ID == DetailsID);
            }
        }



    }
}